{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>happiness</th>\n",
       "      <th>survey_type</th>\n",
       "      <th>province</th>\n",
       "      <th>city</th>\n",
       "      <th>county</th>\n",
       "      <th>survey_time</th>\n",
       "      <th>gender</th>\n",
       "      <th>birth</th>\n",
       "      <th>nationality</th>\n",
       "      <th>...</th>\n",
       "      <th>family_income</th>\n",
       "      <th>family_m</th>\n",
       "      <th>family_status</th>\n",
       "      <th>house</th>\n",
       "      <th>car</th>\n",
       "      <th>marital</th>\n",
       "      <th>status_peer</th>\n",
       "      <th>status_3_before</th>\n",
       "      <th>view</th>\n",
       "      <th>inc_ability</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>32</td>\n",
       "      <td>59</td>\n",
       "      <td>2015/8/4 14:18</td>\n",
       "      <td>1</td>\n",
       "      <td>1959</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>60000.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>18</td>\n",
       "      <td>52</td>\n",
       "      <td>85</td>\n",
       "      <td>2015/7/21 15:04</td>\n",
       "      <td>1</td>\n",
       "      <td>1992</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>29</td>\n",
       "      <td>83</td>\n",
       "      <td>126</td>\n",
       "      <td>2015/7/21 13:24</td>\n",
       "      <td>2</td>\n",
       "      <td>1967</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>8000.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  happiness  survey_type  province  city  county      survey_time  \\\n",
       "0   1          4            1        12    32      59   2015/8/4 14:18   \n",
       "1   2          4            2        18    52      85  2015/7/21 15:04   \n",
       "2   3          4            2        29    83     126  2015/7/21 13:24   \n",
       "\n",
       "   gender  birth  nationality     ...       family_income  family_m  \\\n",
       "0       1   1959            1     ...             60000.0         2   \n",
       "1       1   1992            1     ...             40000.0         3   \n",
       "2       2   1967            1     ...              8000.0         3   \n",
       "\n",
       "   family_status  house  car  marital  status_peer  status_3_before  view  \\\n",
       "0              2      1    2        3            3                2     4   \n",
       "1              4      1    2        1            1                1     4   \n",
       "2              3      1    2        3            2                1     4   \n",
       "\n",
       "   inc_ability  \n",
       "0            3  \n",
       "1            2  \n",
       "2            2  \n",
       "\n",
       "[3 rows x 42 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd \n",
    "import numpy as np \n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "data_train = pd.read_csv('happiness_train_abbr.csv')\n",
    "data_train.head()[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8000 entries, 0 to 7999\n",
      "Data columns (total 42 columns):\n",
      "id                 8000 non-null int64\n",
      "happiness          8000 non-null int64\n",
      "survey_type        8000 non-null int64\n",
      "province           8000 non-null int64\n",
      "city               8000 non-null int64\n",
      "county             8000 non-null int64\n",
      "survey_time        8000 non-null object\n",
      "gender             8000 non-null int64\n",
      "birth              8000 non-null int64\n",
      "nationality        8000 non-null int64\n",
      "religion           8000 non-null int64\n",
      "religion_freq      8000 non-null int64\n",
      "edu                8000 non-null int64\n",
      "income             8000 non-null int64\n",
      "political          8000 non-null int64\n",
      "floor_area         8000 non-null float64\n",
      "height_cm          8000 non-null int64\n",
      "weight_jin         8000 non-null int64\n",
      "health             8000 non-null int64\n",
      "health_problem     8000 non-null int64\n",
      "depression         8000 non-null int64\n",
      "hukou              8000 non-null int64\n",
      "socialize          8000 non-null int64\n",
      "relax              8000 non-null int64\n",
      "learn              8000 non-null int64\n",
      "equity             8000 non-null int64\n",
      "class              8000 non-null int64\n",
      "work_exper         8000 non-null int64\n",
      "work_status        2951 non-null float64\n",
      "work_yr            2951 non-null float64\n",
      "work_type          2951 non-null float64\n",
      "work_manage        2951 non-null float64\n",
      "family_income      7999 non-null float64\n",
      "family_m           8000 non-null int64\n",
      "family_status      8000 non-null int64\n",
      "house              8000 non-null int64\n",
      "car                8000 non-null int64\n",
      "marital            8000 non-null int64\n",
      "status_peer        8000 non-null int64\n",
      "status_3_before    8000 non-null int64\n",
      "view               8000 non-null int64\n",
      "inc_ability        8000 non-null int64\n",
      "dtypes: float64(6), int64(35), object(1)\n",
      "memory usage: 2.6+ MB\n"
     ]
    }
   ],
   "source": [
    "data_train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>happiness</th>\n",
       "      <th>survey_type</th>\n",
       "      <th>province</th>\n",
       "      <th>city</th>\n",
       "      <th>county</th>\n",
       "      <th>gender</th>\n",
       "      <th>birth</th>\n",
       "      <th>nationality</th>\n",
       "      <th>religion</th>\n",
       "      <th>...</th>\n",
       "      <th>family_income</th>\n",
       "      <th>family_m</th>\n",
       "      <th>family_status</th>\n",
       "      <th>house</th>\n",
       "      <th>car</th>\n",
       "      <th>marital</th>\n",
       "      <th>status_peer</th>\n",
       "      <th>status_3_before</th>\n",
       "      <th>view</th>\n",
       "      <th>inc_ability</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>8000.00000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.00000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.00000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>7.999000e+03</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>8000.00000</td>\n",
       "      <td>8000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>4000.50000</td>\n",
       "      <td>3.850125</td>\n",
       "      <td>1.405500</td>\n",
       "      <td>15.155375</td>\n",
       "      <td>42.564750</td>\n",
       "      <td>70.619000</td>\n",
       "      <td>1.53000</td>\n",
       "      <td>1964.707625</td>\n",
       "      <td>1.37350</td>\n",
       "      <td>0.772250</td>\n",
       "      <td>...</td>\n",
       "      <td>6.776050e+04</td>\n",
       "      <td>2.882500</td>\n",
       "      <td>2.595875</td>\n",
       "      <td>1.063625</td>\n",
       "      <td>1.817125</td>\n",
       "      <td>3.234375</td>\n",
       "      <td>2.226125</td>\n",
       "      <td>1.702500</td>\n",
       "      <td>3.30350</td>\n",
       "      <td>1.094875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2309.54541</td>\n",
       "      <td>0.938228</td>\n",
       "      <td>0.491019</td>\n",
       "      <td>8.917100</td>\n",
       "      <td>27.187404</td>\n",
       "      <td>38.747503</td>\n",
       "      <td>0.49913</td>\n",
       "      <td>16.842865</td>\n",
       "      <td>1.52882</td>\n",
       "      <td>1.071459</td>\n",
       "      <td>...</td>\n",
       "      <td>2.909591e+05</td>\n",
       "      <td>1.521835</td>\n",
       "      <td>1.077011</td>\n",
       "      <td>0.789402</td>\n",
       "      <td>0.511825</td>\n",
       "      <td>1.423182</td>\n",
       "      <td>0.971525</td>\n",
       "      <td>0.976147</td>\n",
       "      <td>1.98132</td>\n",
       "      <td>3.410180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>-8.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1921.000000</td>\n",
       "      <td>-8.00000</td>\n",
       "      <td>-8.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-3.000000e+00</td>\n",
       "      <td>-3.000000</td>\n",
       "      <td>-8.000000</td>\n",
       "      <td>-3.000000</td>\n",
       "      <td>-8.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-8.000000</td>\n",
       "      <td>-8.000000</td>\n",
       "      <td>-8.00000</td>\n",
       "      <td>-8.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>2000.75000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>37.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1952.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.300000e+04</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.00000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>4000.50000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>42.000000</td>\n",
       "      <td>73.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>1965.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>3.800000e+04</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>4.00000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>6000.25000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>22.000000</td>\n",
       "      <td>65.000000</td>\n",
       "      <td>104.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>1977.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>7.000000e+04</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>4.00000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>8000.00000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>31.000000</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>134.000000</td>\n",
       "      <td>2.00000</td>\n",
       "      <td>1997.000000</td>\n",
       "      <td>8.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>9.999992e+06</td>\n",
       "      <td>50.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>5.00000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 41 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               id    happiness  survey_type     province         city  \\\n",
       "count  8000.00000  8000.000000  8000.000000  8000.000000  8000.000000   \n",
       "mean   4000.50000     3.850125     1.405500    15.155375    42.564750   \n",
       "std    2309.54541     0.938228     0.491019     8.917100    27.187404   \n",
       "min       1.00000    -8.000000     1.000000     1.000000     1.000000   \n",
       "25%    2000.75000     4.000000     1.000000     7.000000    18.000000   \n",
       "50%    4000.50000     4.000000     1.000000    15.000000    42.000000   \n",
       "75%    6000.25000     4.000000     2.000000    22.000000    65.000000   \n",
       "max    8000.00000     5.000000     2.000000    31.000000    89.000000   \n",
       "\n",
       "            county      gender        birth  nationality     religion  \\\n",
       "count  8000.000000  8000.00000  8000.000000   8000.00000  8000.000000   \n",
       "mean     70.619000     1.53000  1964.707625      1.37350     0.772250   \n",
       "std      38.747503     0.49913    16.842865      1.52882     1.071459   \n",
       "min       1.000000     1.00000  1921.000000     -8.00000    -8.000000   \n",
       "25%      37.000000     1.00000  1952.000000      1.00000     1.000000   \n",
       "50%      73.000000     2.00000  1965.000000      1.00000     1.000000   \n",
       "75%     104.000000     2.00000  1977.000000      1.00000     1.000000   \n",
       "max     134.000000     2.00000  1997.000000      8.00000     1.000000   \n",
       "\n",
       "          ...       family_income     family_m  family_status        house  \\\n",
       "count     ...        7.999000e+03  8000.000000    8000.000000  8000.000000   \n",
       "mean      ...        6.776050e+04     2.882500       2.595875     1.063625   \n",
       "std       ...        2.909591e+05     1.521835       1.077011     0.789402   \n",
       "min       ...       -3.000000e+00    -3.000000      -8.000000    -3.000000   \n",
       "25%       ...        1.300000e+04     2.000000       2.000000     1.000000   \n",
       "50%       ...        3.800000e+04     3.000000       3.000000     1.000000   \n",
       "75%       ...        7.000000e+04     4.000000       3.000000     1.000000   \n",
       "max       ...        9.999992e+06    50.000000       5.000000    30.000000   \n",
       "\n",
       "               car      marital  status_peer  status_3_before        view  \\\n",
       "count  8000.000000  8000.000000  8000.000000      8000.000000  8000.00000   \n",
       "mean      1.817125     3.234375     2.226125         1.702500     3.30350   \n",
       "std       0.511825     1.423182     0.971525         0.976147     1.98132   \n",
       "min      -8.000000     1.000000    -8.000000        -8.000000    -8.00000   \n",
       "25%       2.000000     3.000000     2.000000         1.000000     3.00000   \n",
       "50%       2.000000     3.000000     2.000000         2.000000     4.00000   \n",
       "75%       2.000000     3.000000     3.000000         2.000000     4.00000   \n",
       "max       2.000000     7.000000     3.000000         3.000000     5.00000   \n",
       "\n",
       "       inc_ability  \n",
       "count  8000.000000  \n",
       "mean      1.094875  \n",
       "std       3.410180  \n",
       "min      -8.000000  \n",
       "25%       2.000000  \n",
       "50%       2.000000  \n",
       "75%       3.000000  \n",
       "max       4.000000  \n",
       "\n",
       "[8 rows x 41 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_train.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_train['family_income'] = data_train['family_income'].fillna(data_train['family_income'].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8000 entries, 0 to 7999\n",
      "Data columns (total 42 columns):\n",
      "id                 8000 non-null int64\n",
      "happiness          8000 non-null int64\n",
      "survey_type        8000 non-null int64\n",
      "province           8000 non-null int64\n",
      "city               8000 non-null int64\n",
      "county             8000 non-null int64\n",
      "survey_time        8000 non-null object\n",
      "gender             8000 non-null int64\n",
      "birth              8000 non-null int64\n",
      "nationality        8000 non-null int64\n",
      "religion           8000 non-null int64\n",
      "religion_freq      8000 non-null int64\n",
      "edu                8000 non-null int64\n",
      "income             8000 non-null int64\n",
      "political          8000 non-null int64\n",
      "floor_area         8000 non-null float64\n",
      "height_cm          8000 non-null int64\n",
      "weight_jin         8000 non-null int64\n",
      "health             8000 non-null int64\n",
      "health_problem     8000 non-null int64\n",
      "depression         8000 non-null int64\n",
      "hukou              8000 non-null int64\n",
      "socialize          8000 non-null int64\n",
      "relax              8000 non-null int64\n",
      "learn              8000 non-null int64\n",
      "equity             8000 non-null int64\n",
      "class              8000 non-null int64\n",
      "work_exper         8000 non-null int64\n",
      "work_status        2951 non-null float64\n",
      "work_yr            2951 non-null float64\n",
      "work_type          2951 non-null float64\n",
      "work_manage        2951 non-null float64\n",
      "family_income      8000 non-null float64\n",
      "family_m           8000 non-null int64\n",
      "family_status      8000 non-null int64\n",
      "house              8000 non-null int64\n",
      "car                8000 non-null int64\n",
      "marital            8000 non-null int64\n",
      "status_peer        8000 non-null int64\n",
      "status_3_before    8000 non-null int64\n",
      "view               8000 non-null int64\n",
      "inc_ability        8000 non-null int64\n",
      "dtypes: float64(6), int64(35), object(1)\n",
      "memory usage: 2.6+ MB\n"
     ]
    }
   ],
   "source": [
    "data_train.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression \n",
    "from sklearn.cross_validation import KFold \n",
    "\n",
    "predictors = ['survey_type', 'province', 'city', 'gender', 'birth', 'nationality', 'religion', 'religion_freq', 'edu',\n",
    "             'income', 'political', 'floor_area', 'height_cm', 'weight_jin', 'health', 'health_problem', 'depression',\n",
    "             'hukou', 'socialize', 'relax', 'learn', 'equity', 'class', #'work_exper', 'work_status', 'work_yr', 'work_type', 'work_manage', \n",
    "             'family_income', 'family_m', 'family_status', 'house', 'car', 'marital', 'status_peer',\n",
    "             'status_3_before', 'view', 'inc_ability' ]\n",
    "alg = LinearRegression()\n",
    "kf = KFold(data_train.shape[0], shuffle=False, random_state=1)\n",
    "\n",
    "predictions = []\n",
    "for train, test in kf:\n",
    "    train_predictors = data_train[predictors].iloc[train, :]\n",
    "    train_target = data_train['happiness'].iloc[train]\n",
    "    alg.fit(train_predictors, train_target)\n",
    "    test_predictions = alg.predict(data_train[predictors].iloc[test, :])\n",
    "    predictions.append(test_predictions)\n",
    "#     print(data_train['happiness'].iloc[train] - test_predictions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4., 4., 4., ..., 4., 4., 4.])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions = np.concatenate(predictions, axis=0)\n",
    "np.around(predictions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0738977490360095"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum((np.around(predictions) - predictions)**2)/8000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_test = pd.read_csv('happiness_test_abbr.csv')\n",
    "# data_test.info()\n",
    "# data_test.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4., 3., 3., ..., 4., 4., 5.])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_prediction = np.around(alg.predict(data_test[predictors]))\n",
    "test_prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit = pd.read_csv('happiness_submit.csv')\n",
    "submit['happiness'] = test_prediction\n",
    "submit.to_csv('happiness_submit.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 随机森林"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 4, 5, ..., 4, 4, 4], dtype=int64)"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import model_selection \n",
    "from sklearn.ensemble import RandomForestClassifier \n",
    "from sklearn import tree\n",
    "\n",
    "predictors = ['survey_type', 'province', 'city', 'gender', 'birth', 'nationality', 'religion', 'religion_freq', 'edu',\n",
    "             'income', 'political', 'floor_area', 'height_cm', 'weight_jin', 'health', 'health_problem', 'depression',\n",
    "             'hukou', 'socialize', 'relax', 'learn', 'equity', 'class', #'work_exper', 'work_status', 'work_yr', 'work_type', 'work_manage', \n",
    "             'family_income', 'family_m', 'family_status', 'house', 'car', 'marital', 'status_peer',\n",
    "             'status_3_before', 'view', 'inc_ability' ]\n",
    "clf = RandomForestClassifier(n_estimators=10)\n",
    "clf.fit(data_train[predictors][:i], data_train['happiness'][:i])\n",
    "predictions = clf.predict(data_train[predictors][i:])\n",
    "predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5885"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(predictions == data_train['happiness'][6000:])/len(predictions)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
